package com.ihd.ihardware.view.tzc.discovery.view;

import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.ihd.ihardware.R;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.databinding.DiscoveryGzFragmentBinding;
import com.ihd.ihardware.pojo.RecommendRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.view.tzc.discovery.viewmodel.DiscoveryViewModel;

import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.base.BaseFragment;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class GZFragment extends BaseFragment<DiscoveryGzFragmentBinding, DiscoveryViewModel> {

    private Disposable msg;
    private Disposable msg0;

    public GZFragment() {
    }

    public static GZFragment newInstance(String param1, String param2) {
        GZFragment fragment = new GZFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        setRx(AppConstans.RECOMMEND, new BaseConsumer<RecommendRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(RecommendRes mLiveData) {
                viewModel.setConcernList(mLiveData.getData());
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        setRx(AppConstans.CONCERN_SIGNS, new BaseConsumer<SignsRes>(binding.stateLayout) {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(SignsRes mLiveData) {
                binding.refresh.setRefreshing(false);
                viewModel.setDiscoveryList(mLiveData.getData().getSignDetails());
                binding.concernedRV.setHasFixedSize(true);
                binding.concernedRV.setNestedScrollingEnabled(false);
                if (mLiveData.getData().getSignDetails().size() == 0)
                    binding.stateLayout.showEmptyView();
            }

            @Override
            protected void onError(Throwable error) {
            }
        });

        msg = RxBus.getInstance().tObservable(AppConstans.CONCERN, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("key") == 0) {
                            ObservableArrayList<RecommendRes.DataBean> sds = (ObservableArrayList<RecommendRes.DataBean>) viewModel.mCommonAdapter.getItems();
                            for (int i = 0; i < sds.size(); i++) {
                                if (sds.get(i).getUserId().equals(b.getString("value"))) {
                                    if (b.getInt("type") == 0) {
                                        sds.get(i).setConcern(false);
                                    } else {
                                        sds.get(i).setConcern(true);
                                    }
                                    viewModel.mCommonAdapter.notifyItemChanged(i);
                                    viewModel.getDiscoveryList(1, 100);
                                }
                            }
                        } else
                            ToastUtils.showShort(getContext(), b.getString("value"));
                    }
                });

        msg0 = RxBus.getInstance().tObservable(AppConstans.TAG, Bundle.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {
                    @Override
                    public void accept(Bundle b) throws Exception {
                        if (b.getInt("key") == 0) {
                            ObservableArrayList<SignsRes.DataBean.SignDetailsBean> sds = (ObservableArrayList<SignsRes.DataBean.SignDetailsBean>) viewModel.mConcernedAdapter.getItems();
                            for (int i = 0; i < sds.size(); i++) {
                                if (sds.get(i).getSignId().equals(b.getString("value"))) {
                                    if (b.getInt("type") == 0) {
                                        sds.get(i).setTagsNum(String.valueOf(Integer.parseInt(sds.get(i).getTagsNum()) - 1));
                                        sds.get(i).setTaged(false);
                                    } else {
                                        sds.get(i).setTagsNum(String.valueOf(Integer.parseInt(sds.get(i).getTagsNum()) + 1));
                                        sds.get(i).setTaged(true);
                                    }
                                    viewModel.mConcernedAdapter.notifyItemChanged(i);
                                }
                            }
                        } else
                            ToastUtils.showShort(getContext(), b.getString("value"));
                    }
                });

        binding.introduceRV.setAdapter(viewModel.mCommonAdapter);
        binding.concernedRV.setAdapter(viewModel.mConcernedAdapter);

        binding.faBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getContext(), DTActivity.class);
                startActivity(i);
            }
        });

        binding.refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                viewModel.getDiscoveryList(1, 100);
            }
        });
    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        viewModel.getConcernList();
        viewModel.getDiscoveryList(1, 100);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.discovery_gz_fragment;
    }

    @Override
    protected Class getViewModelClass() {
        return DiscoveryViewModel.class;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (msg != null && !msg.isDisposed())
            msg.dispose();
        if (msg0 != null && !msg0.isDisposed())
            msg0.dispose();
    }
}
